<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
      <el-steps :active="active" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="地址信息"></el-step>
        <el-step title="其他信息"></el-step>
        <!-- <el-step title="销售信息"></el-step> -->
      </el-steps>
      <br />
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <div v-show="active==1" style="margin-top: 20px;">
            <el-col :span="12" v-if="sysNo==1">
              <el-form-item label="店铺" prop="shop.id" :rules="[
                 ]">
                <GridSelect title="选择店铺ID" labelName="shopName" labelValue="id" :value="inputForm.shop.id" :limit="1"
                  @getValue="(value) => {inputForm.shop.id=value}" :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'shopName',
              label: '门店名称'
            },
            {
              prop: 'contacts',
              label: '联系人'
            },
            {
              prop: 'contactsPhone',
              label: '联系人电话'
            }
            ]" :searchs="[
            {
              prop: 'shopName',
              label: '门店名称'
            }
            ]" data-list-url="/tshopinfo/tShopInfo/list" entityBeanName="tShopInfo"
                  queryEntityUrl="/tshopinfo/tShopInfo/queryById"></GridSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="title" :rules="[
                 ]">
                <el-input v-model="inputForm.title" placeholder="请填写名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开盘时间" prop="starTime" :rules="[
                 ]">
                <el-date-picker style="width: 100%;" v-model="inputForm.starTime" type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="交房时间" prop="giveTime" :rules="[
                 ]">
                <el-date-picker style="width: 100%;" v-model="inputForm.giveTime" type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="起价" prop="starPrice" :rules="[
                 ]">
                <el-input v-model="inputForm.starPrice" placeholder="请填写起价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label=" 均价" prop="averagePrice" :rules="[
                 ]">
                <el-input v-model="inputForm.averagePrice" placeholder="请填写 均价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规划户数" prop="householdsNum" :rules="[
                 ]">
                <el-input v-model="inputForm.householdsNum" placeholder="请填写规划户数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规划车位" prop="vehiclesNum" :rules="[
                 ]">
                <el-input v-model="inputForm.vehiclesNum" placeholder="请填写规划车位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规划楼座数" prop="buildingsNum" :rules="[
                 ]">
                <el-input v-model="inputForm.buildingsNum" placeholder="请填写规划楼座数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物业费" prop="propertyFee" :rules="[
                 ]">
                <el-input v-model="inputForm.propertyFee" placeholder="请填写物业费"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="建筑类型" prop="erectType" :rules="[
                 ]">
                 <el-select v-model="erectType" multiple placeholder="请选择建筑类型" size="small" style="width: 100%;">
                  <el-option v-for="item in valueList" v-if="item.type==19" :key="item.valueName" :label="item.valueName"
                    :value="item.valueName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年限" prop="years" :rules="[
                 ]">
                
     
        <el-select v-model="inputForm.years" placeholder="请选择年限" size="small" style="width: 100%;">
                  <el-option v-for="item in valueList" v-if="item.type==21" :key="item.valueName" :label="item.valueName"
                    :value="item.valueName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="容积率" prop="plotRatio" :rules="[
                 ]">
                <el-input v-model="inputForm.plotRatio" placeholder="请填写容积率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="绿化率" prop="greenRate" :rules="[
                 ]">
                <el-input v-model="inputForm.greenRate" placeholder="请填写绿化率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label=" 占地面积" prop="areaCovered" :rules="[
                 ]">
                <el-input v-model="inputForm.areaCovered" placeholder="请填写 占地面积"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="建筑面积" prop="floorage" :rules="[
                 ]">
                <el-input v-model="inputForm.floorage" placeholder="请填写建筑面积"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="投资商" prop="investor" :rules="[
                 ]">
                <el-input v-model="inputForm.investor" placeholder="请填写投资商"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物业公司" prop="propertyCompany" :rules="[
                 ]">
                <el-input v-model="inputForm.propertyCompany" placeholder="请填写物业公司"></el-input>
              </el-form-item>
            </el-col>
          </div>
          <div v-show="active==2" style="margin-top: 20px;">
            <el-col :span="12">
              <el-form-item label="地址" prop="address" :rules="[
               ]">
                <el-input v-model="inputForm.address" ref="handleinput" id="handleinput" placeholder="请填写地址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="经度" prop="longitude" :rules="[
               ]">
                <el-input v-model="inputForm.longitude" disabled placeholder="请填写经度"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纬度" prop="latitude" :rules="[
               ]">
                <el-input v-model="inputForm.latitude" disabled placeholder="请填写纬度"></el-input>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="地图" prop>
                <div ref="container" id="containepic"></div>
              </el-form-item>
            </el-col>
          </div>
          <div v-show="active==3" style="margin-top: 20px;">
            <!-- <el-col :span="24" v-for="(domain, index) in domains" :key="index">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>预售许可</span>
                  <el-button style="float: right; padding: 3px 0" v-if="domain.key==1" @click="addDomain" type="text">新增
                  </el-button>
                  <el-button style="float: right; padding: 3px 0" v-if="domain.key!=1"
                    @click.prevent="removeDomain(domain)" type="text">删除</el-button>
                </div>
                <el-col :span="12">
                  <el-form-item label="平方注字" prop="permit" :rules="[
                 ]">
                    <el-input v-model="domain.pfzz" placeholder="请填写平方注字"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="发证时间" prop="permit" :rules="[
                 ]">
                    <el-input v-model="domain.fzsj" placeholder="请填写发证时间"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绑定楼号" prop="permit" :rules="[
                 ]">
                    <el-input v-model="domain.bdlh" placeholder="请填写绑定楼号"></el-input>
                  </el-form-item>
                </el-col>
              </el-card>
            </el-col> -->
            <el-col :span="24">
              <el-form-item label="房源视频" prop="showVideo" :rules="[
                {required: true, message:'视频不能为空', trigger:'blur'}
               ]">
                <el-upload ref="showVideo" v-if="visible"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/buildingsinfo/tBuildingsInfo`"
                  :headers="{token: $cookie.get('token')}"
                  :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                  :on-success="(response, file, fileList) => {
                     inputForm.showVideo = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                  }" :on-remove="(file, fileList) => {
                    $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                      $message.success(data.msg)
                    })
                    inputForm.showVideo = fileList.map(item => item.url).join('|')
                  }" :before-remove="(file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`)
                  }" :limit="1" :on-exceed="(files, fileList) =>{
                    $message.warning(`当前限制上传1个视频`)
                  }" :file-list="showVideoArra">
                  <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片" prop="showImg" :rules="[
                 ]">
                <el-upload ref="showImg" v-if="visible" list-type="picture-card"
                  :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/buildingsinfo/tBuildingsInfo`"
                  :headers="{token: $cookie.get('token')}" :on-preview="(file, fileList) => {
                    showImg(file.url)
                    }" :on-success="(response, file, fileList) => {
                       inputForm.showImg = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }" :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.showImg = fileList.map(item => item.url).join('|')
                    }" :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }" multiple :limit="5" :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }" :file-list="showImgArra">
                  <i class="el-icon-plus"></i>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注信息" prop="remarks" :rules="[
               ]">
                <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
              </el-form-item>
            </el-col>
          </div>
          <!-- <div v-show="active==4" style="margin-top: 20px;">
            <el-col :span="24" v-for="(buildingsNumber, index) in buildingsNumbers">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>楼栋信息</span>
                  <el-button style="float: right; padding: 3px 0" v-if="buildingsNumber.key==1"
                    @click="addBuildingsNumber" type="text">新增</el-button>
                  <el-button style="float: right; padding: 3px 0" v-if="buildingsNumber.key!=1"
                    @click.prevent="removeBuildingsNumber(buildingsNumber)" type="text">删除</el-button>
                </div>
                <el-col :span="12">
                  <el-form-item label="开盘时间" prop="openingTime" :rules="[
                 ]">
                    <el-date-picker style="width: 100%;" v-model="buildingsNumber.openingTime" type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="交房时间" prop="deliveryTime" :rules="[
                 ]">
                    <el-date-picker style="width: 100%;" v-model="buildingsNumber.deliveryTime" type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="单元数" prop="unitNumber" :rules="[
                 ]">
                    <el-input v-model="buildingsNumber.unitNumber" placeholder="请填写单元数"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="层数" prop="layerNumber" :rules="[
                 ]">
                    <el-input v-model="buildingsNumber.layerNumber" placeholder="请填写层数"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="户数" prop="housesNumber" :rules="[
                 ]">
                    <el-input v-model="buildingsNumber.housesNumber" placeholder="请填写户数"></el-input>
                  </el-form-item>
                </el-col>
              </el-card>
            </el-col>
          </div> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="active !== 1" style="margin-top: 12px;" @click="upper">上一步</el-button>
        <el-button v-if="active !== 3" style="margin-top: 12px;" @click="next">下一步</el-button>
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import GridSelect from "@/components/gridSelect";
  export default {
    data() {
      return {
        sysNo: localStorage.getItem("sysNo"),
        title: "",
        active: 1,
        method: "",
        erectType:[],
        visible: false,
        loading: false,
        showVideoArra: [],
        showImgArra: [],
        domains: [
          {
            key: 1,
            pfzs: "",
            fzsj: "",
            bdlh: ""
          }
        ],
        buildingsNumbers: [
          {
            key: 1,
            openingTime: "",
            deliveryTime: "",
            unitNumber: "",
            layerNumber: "",
            housesNumber: ""
          }
        ],
        valueList:[],
        inputForm: {
          id: "",
          shop: {
            id: ""
          },
          title: "",
          address: "",
          starTime: "",
          giveTime: "",
          starPrice: "",
          averagePrice: "",
          householdsNum: "",
          vehiclesNum: "",
          buildingsNum: "",
          propertyFee: "",
          erectType: "",
          years: "",
          plotRatio: "",
          greenRate: "",
          areaCovered: "",
          floorage: "",
          investor: "",
          propertyCompany: "",
          longitude: "",
          latitude: "",
          permit: "",
          showVideo: "",
          showImg: "",
          status: "",
          remarks: ""
        },
        markersArray: []
      };
    },
    components: {
      GridSelect
    },
    created:{
      // this.$http
    },
    activated() {
      this.getvalueList()
    },
    methods: {
      showImg(url) {
      this.$hevueImgPreview(url)
    },
       // 获取数据列表
      getvalueList() {
        this.$http({
          url: '/valueinfo/tValueInfo/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 999,
            'orderBy': this.orderBy,
            'type': '21,19'
          }
        }).then(({ data }) => {
          if (data && data.success) {
            this.valueList = data.page.list
          }
        })
      },
      removeDomain(item) {
        console.log(item);
        var index = this.domains.indexOf(item);
        console.log(index, "aaaaaaa");
        if (index !== -1) {
          this.domains.splice(index, 1);
        }
      },
      addDomain() {
        this.domains.push({
          key: this.domains.length + 1,
          pfzs: "",
          fzsj: "",
          bdlh: ""
        });
      },
      next() {
        this.active++;
      },
      upper() {
        this.active--;
      },
      initMap(longitude, latitude) {
        var that = this;
        let address = "";
        let lat = "36.752508224779746";
        let logt = "119.97121810913086";
        if (!longitude) {
          let lat = latitude;
          let logt = longitude;
        }
        if (this.inputForm.latitude === "") {
          var center = new qq.maps.LatLng(lat, logt);
        } else {
          var center = new qq.maps.LatLng(
            this.inputForm.latitude,
            this.inputForm.longitude
          );
        }
        var map = new qq.maps.Map(document.getElementById("containepic"), {
          center: center,
          zoom: 13,
          disableDefaultUI: true
        });
        var marker = new qq.maps.Marker({
          position: center,
          map: map
        });
        that.markersArray.push(marker);
        var infoWin = new qq.maps.InfoWindow({
          map: map
        }); //此处是点击地图后生成的地标名称
        var geocoder = new qq.maps.Geocoder({
          complete: function (res) {
            that.deleteOverlays();
            map.setCenter(res.detail.location);
            var marker = new qq.maps.Marker({
              map: map,
              position: res.detail.location
            });
            that.markersArray.push(marker);
          }
        });

        qq.maps.event.addListener(map, "click", function (event) {
          that.deleteOverlays();
          this.latitude = event.latLng.getLat();
          this.longiistude = event.latLng.getLng();
          console.log(this.longiistude, this.latitude);
          that.inputForm.longitude = this.longiistude;
          that.inputForm.latitude = this.latitude;
          var position = new qq.maps.LatLng(
            that.inputForm.latitude,
            that.inputForm.longitude
          );
          var marker = new qq.maps.Marker({
            map: map,
            position: position
          });
          that.markersArray.push(marker);
        });
        document
          .getElementById("handleinput")
          .addEventListener("blur", function () {
            console.log("1111");
            geocoder.getLocation(that.inputForm.address);
          });
      },
      deleteOverlays() {
        if (this.markersArray) {
          for (var i in this.markersArray) {
            this.markersArray[i].setMap(null);
          }
          this.markersArray.length = 0;
        }
      },
      init(method, id) {
        this.method = method;
        this.inputForm.id = id;
        this.active = '1'
        if (method === "add") {
          this.title = `新建楼盘`;
        } else if (method === "edit") {
          this.title = "修改楼盘";
        } else if (method === "view") {
          this.title = "查看楼盘";
        }
        this.showVideoArra = [];
        this.showImgArra = [];
        this.visible = true;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields();
          if (method === "edit" || method === "view") {
            // 修改或者查看
            this.loading = true;
            this.$http({
              url: `/buildingsinfo/tBuildingsInfo/queryById?id=${this.inputForm.id}`,
              method: "get"
            }).then(({ data }) => {
              this.inputForm = this.recover(this.inputForm, data.tBuildingsInfo);
              this.initMap(this.inputForm.longiistude, this.inputForm.latitude);
              console.log(this.inputForm.permit);
              this.erectType = this.inputForm.erectType.split(',');
              if(this.inputForm.permit){
                console.log(JSON.parse(decodeURI(this.inputForm.permit)));
                if (JSON.parse(decodeURI(this.inputForm.permit)).length > 0){
                  this.domains = JSON.parse(decodeURI(this.inputForm.permit));
                }
              }
              if(this.inputForm.buildingsNumbers){
                this.buildingsNumbers = JSON.parse(this.inputForm.buildingsNumbers);
              }
              this.inputForm.showVideo.split("|").forEach(item => {
                if (item.trim().length > 0) {
                  this.showVideoArra.push({
                    name: decodeURIComponent(
                      item.substring(item.lastIndexOf("/") + 1)
                    ),
                    url: item
                  });
                }
              });
              this.inputForm.showImg.split("|").forEach(item => {
                if (item.trim().length > 0) {
                  this.showImgArra.push({
                    name: decodeURIComponent(
                      item.substring(item.lastIndexOf("/") + 1)
                    ),
                    url: item
                  });
                }
              });
              this.loading = false;
            });
          } else {
            this.domains = [
              {
                key: 1,
                pfzs: "",
                fzsj: "",
                bdlh: ""
              }
            ],
            this.initMap();
          }
        });
      },
      // 表单提交
      doSubmit() {
        this.$refs["inputForm"].validate(valid => {
          if (valid) {
            this.inputForm.permit = encodeURI(JSON.stringify(this.domains));
            this.inputForm.erectType = this.erectType.join(',')
            this.loading = true;
            this.$http({
              url: `/buildingsinfo/tBuildingsInfo/save`,
              method: "post",
              data: this.inputForm
            }).then(({ data }) => {
              this.loading = false;
              if (data && data.success) {
                this.visible = false;
                this.$message.success(data.msg);
                this.$emit("refreshDataList");
              }
            });
          }
        });
      }
    }
  };
</script>
<style>
  #containepic {
    width: 750px;
    height: 500px;
  }
</style>
<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    margin-left: 50%;
    width: 820px;
    transform: translateX(-50%);
    margin-bottom: 20px;
  }
</style>
<style lang="less" scoped>
.showPic .el-message-box__message{
    width: 800px;
    height: 800px;
    overflow: hidden;
    display: flex;
     p img {
      height: 100%!important;
      width: 100%!important;
      object-fit: contain;
    }
  }
  </style>